//
//  StatusOriginalView.swift
//  SinaWeibo
//
//  Created by 张子国 on 15/11/16.
//  Copyright © 2015年 张子国. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class StatusOriginalView: UIView {

    //记录微博底部约束
    var bottomConstraints : Constraint?
    //数据模型
    var status: Status? {
        didSet {
//          print("进来－－－－－－－－－－")
            iconImage.sd_setImageWithURL(status?.user?.headImage, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            memberIconView.image = status?.user?.memberImage
            verifiedIconView.image = status?.user?.verfied_type_Image
            timeLabel.text = status?.created_at
            sourceLabel.text = status?.source
            contentLabel.text = status?.text
//          pictureView.imageUrls = status?.imageURLs
            
            //SnapKit 中去除约束
            self.bottomConstraints?.uninstall()
            
            if let picurls = status?.imageURLs where picurls.count > 0 {
                //有配图
                pictureView.imageUrls = status?.imageURLs
                //显示视图
                pictureView.hidden = false
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                 self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                //没有配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: 设置UI
    private func setupUI() {
        backgroundColor = UIColor(white: 0.9, alpha: 1)
//        iconImage.layer.cornerRadius = (iconImage.bounds.width / 2)
//        iconImage.layer.masksToBounds = true
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(verifiedIconView)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        sepView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(sepView)
        
        //设置约束
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.height.equalTo(StatusCellIconWitdth)
            make.width.equalTo(StatusCellIconWitdth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }

        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImage)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        
        }
        
        
        //设置原创微博底部约束
        self.snp_makeConstraints { (make) -> Void in
        self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    //MARK: 懒加载控件
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(title: "你好女神", size: 17, color: UIColor.darkGrayColor())
    //用户类型认证
    private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级
    private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 10, color: UIColor.orangeColor())
    ///微博来源
    
    private lazy var sourceLabel: UILabel = UILabel(title: "来自: 喜马拉雅", size: 10, color: UIColor.lightGrayColor())
    ///微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "我是神经病", size: 14, color: UIColor.lightGrayColor())
    private lazy var pictureView : StatusPictureView = StatusPictureView()
  
    private lazy var sepView : UIView = UIView()
}


